/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUICadastroEspeciePagamento.java
 *
 * Created on 17/02/2011, 22:12:01
 */

package visao.gui;

import excecao.PessoaisException;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.lang.String;
import java.lang.String;
import javax.swing.JFrame;
import modelo.dominio.EspeciePagamento;
import modelo.dominio.constante.Constante;
import modelo.dominio.constante.OperacaoEspeciePagamento;
import modelo.dominio.constante.TipoContaEspeciePagamento;
import persistencia.impl.GeradorDeChave;

/**
 *
 * @author Marlon
 */
public class GUICadastroEspeciePagamento extends javax.swing.JInternalFrame {

    /** Creates new form GUICadastroEspeciePagamento */
    public GUICadastroEspeciePagamento() {
        initComponents();
        for (OperacaoEspeciePagamento operacao_especie_pagamento : OperacaoEspeciePagamento.values() ){
          cbOperacao.addItem(operacao_especie_pagamento.name());
        }

        for (TipoContaEspeciePagamento tipo_conta_especie_pagamento : TipoContaEspeciePagamento.values() ){
          cbTipoConta.addItem(tipo_conta_especie_pagamento.name());
        }
    }

    public void setPosicao() {
         Dimension d = this.getDesktopPane().getSize();
         this.setLocation(15 + (d.width - this.getSize().width) / 5, 15 + (d.height - this.getSize().height) / 5);
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        bSair = new javax.swing.JButton();
        bNovo = new javax.swing.JButton();
        bGravar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        lbCodigo = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        edtDescricao = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cbOperacao = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cbTipoConta = new javax.swing.JComboBox();

        setClosable(true);
        setTitle("Cadastro de Espécie");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(176, 176, 175));
        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        bSair.setBackground(new java.awt.Color(176, 176, 175));
        bSair.setText("Sair");
        bSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSairActionPerformed(evt);
            }
        });

        bNovo.setBackground(new java.awt.Color(176, 176, 175));
        bNovo.setText("Novo");
        bNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bNovoActionPerformed(evt);
            }
        });

        bGravar.setBackground(new java.awt.Color(176, 176, 175));
        bGravar.setText("Gravar");
        bGravar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGravarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(bNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bGravar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bSair, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(223, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(bGravar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(bNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(bSair, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jLabel1.setText("Código Espécie");

        lbCodigo.setFont(new java.awt.Font("sansserif", 1, 12));
        lbCodigo.setForeground(new java.awt.Color(0, 0, 255));
        lbCodigo.setText("codigo");

        jLabel2.setText("Descrição");

        jLabel3.setText("Operação");

        cbOperacao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione.." }));

        jLabel4.setText("Tipo de Conta");

        cbTipoConta.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione.." }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbCodigo)
                .addContainerGap(272, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbTipoConta, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbOperacao, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(edtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(95, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbCodigo)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(edtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbOperacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbTipoConta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(28, 28, 28))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSairActionPerformed
       this.dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_bSairActionPerformed

    private void bNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bNovoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bNovoActionPerformed

    private void bGravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGravarActionPerformed
      dispose();   // TODO add your handling code here:
    }//GEN-LAST:event_bGravarActionPerformed

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
     // TODO add your handling code here:
    }//GEN-LAST:event_formComponentResized

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed

    }//GEN-LAST:event_formInternalFrameClosed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bGravar;
    private javax.swing.JButton bNovo;
    private javax.swing.JButton bSair;
    private javax.swing.JComboBox cbOperacao;
    private javax.swing.JComboBox cbTipoConta;
    private javax.swing.JTextField edtDescricao;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbCodigo;
    // End of variables declaration//GEN-END:variables

   private EspeciePagamento especie_pagamento;

   private GeradorDeChave   codigo_especie;


   public EspeciePagamento getEspeciePagamento() throws PessoaisException {

 
       String operacao =  (String) cbOperacao.getSelectedItem();

       if (operacao == "") {
          throw new PessoaisException("Não foi informada a operacao.");
       } else {
               especie_pagamento.setOperacao(operacao);
              }
        String descricao = edtDescricao.getText();

        if (descricao == null || descricao.trim().equals("")){
           edtDescricao.requestFocus();
           throw new PessoaisException("Não foi informado o nome do sócio.");
        } else {
            especie_pagamento.setDescricao(descricao);
          }
        String tipo_conta = (String) cbTipoConta.getSelectedItem();

        if (tipo_conta == "") {
            throw new PessoaisException("Nao foi informado o tipo de conta");
        } else
            especie_pagamento.setTipo_conta(tipo_conta);


        return especie_pagamento;
   }

   public void limparCampos(){
     cbOperacao.setSelectedIndex(0);
     edtDescricao.setText(null);
     cbTipoConta.setSelectedIndex(0);
   }


   public void exibeDadosEspeciePagamento() {
      limparCampos();
      if (especie_pagamento.getId_especie_pagamento() != Constante.NOVO) {
          
          edtDescricao.setText(especie_pagamento.getDescricao());

          String  tipo_conta =  especie_pagamento.getTipo_conta().trim();

          int i = 0 ;
          for (TipoContaEspeciePagamento tipo_conta_especie_pagamento : TipoContaEspeciePagamento.values() ){
             i = i + 1;
             if (tipo_conta_especie_pagamento.name().trim().equals(tipo_conta))
                cbTipoConta.setSelectedIndex(i);
          }
          i= 0;
          String operacao = especie_pagamento.getOperacao().trim();
          for (OperacaoEspeciePagamento operacao_especie_pagamento : OperacaoEspeciePagamento.values()){
            i = i+ 1;
            if (operacao_especie_pagamento.name().trim().equals(operacao) )
              cbOperacao.setSelectedIndex(i);
          }
          
       }
      lbCodigo.setText(String.valueOf(especie_pagamento.getId_especie_pagamento()));
   }

   public void setEspeciePagamento(EspeciePagamento especie_pagamento){
      this.especie_pagamento = especie_pagamento;
      this.exibeDadosEspeciePagamento();
   }

   public void bGravarEspeciePagamentoAddActionListener(ActionListener ouvinte) {
     bGravar.addActionListener(ouvinte);
    
   }
}
